package com.liusand.controller;

import com.liusand.common.Result;
import com.liusand.dto.SetmealDto;
import com.liusand.pojo.PageInfo;
import com.liusand.pojo.Setmeal;
import com.liusand.service.SetmealService;
import com.liusand.vo.SetmealAndDishVo;
import com.liusand.vo.SetmealVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.controller
 * fileName :SetmealController
 * creationTime :2022/11/25 20:53
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public Result saveSetmeal(@RequestBody SetmealDto setmealDto){
        if(setmealDto == null){
            return Result.error("添加失败");
        }
        boolean re = setmealService.saveSetmeal(setmealDto);
        if(!re){
            return Result.error("添加失败");
        }
        return Result.success();
    }

    @GetMapping("page")
    public Result page(String name,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        PageInfo<SetmealVo> info = setmealService.page(name, page, pageSize);
        if(info == null){
            return Result.error("查询失败");
        }
        return Result.success(info);
    }

    @GetMapping("{id}")
    public Result findSetmealById(@PathVariable String id){
        if(id == null){
            return Result.error("查询失败");
        }
        SetmealAndDishVo setmealAndDishVo = setmealService.findSetmealById(id);
        if(setmealAndDishVo == null){
            return Result.error("查询失败");
        }
        return Result.success(setmealAndDishVo);
    }

    @DeleteMapping
    public Result deleteSetmeal(@RequestParam("ids") String id){
        if(id == null){
            return Result.error("删除失败");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>(Arrays.asList(split));
        boolean re = setmealService.deleteSetmeal(ids);
        if(!re){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDto setmealDto){
        if(setmealDto == null){
            return Result.error("更新失败");
        }
        boolean re = setmealService.updateSetmeal(setmealDto);
        if(!re){
            return Result.error("更新失败");
        }
        return Result.success();
    }

    @PostMapping("status/{status}")
    public Result updateStatus(@RequestParam("ids") String id, @PathVariable Integer status){
        if(id == null || status == null){
            return Result.error("修改状态失败");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>(Arrays.asList(split));
        boolean re = setmealService.updateStatus(ids, status);
        if(!re){
            return Result.error("修改状态失败");
        }
        return Result.success("操作成功");
    }
}
